<?php

// no direct access
defined( '_JEXEC' ) or die( 'Restricted access' );

jimport('joomla.application.component.model');

class SyncrModelReceive extends JModel
{		
	function __construct() {
		parent::__construct();
		
		// new class
		$evl = new evolveLibrary;
		
		// load some evl stuff
		$this->_log = $evl->loadHelper('log');
	}
	
	public function getData()
	{
		
		$this->_log->simpleLog('categories.php/_buildQuery: could not find categories', 'syncr');
		
		if (empty($post)) {
			$post['data'] = JRequest::getString('data', '', 'POST', JREQUEST_ALLOWRAW);
			$post['processId'] =  JRequest::getInt('processId', 0, 'POST');
			$post['categoryid'] = (int) JRequest::getInt('categoryId', '', 'POST');
		}
				
		if (!$this->store($post, $this->_checkId($post))) {
			JError::raiseError(500, JText::_('Could not recieve content'));
		}
	}

	public function store($post, $update)
	{
		$row =& $this->getTable('content', 'Table');
		
		$json = json_decode($post['data']);
		
		print_r($post);
		
		$data = array();
		$data['process_id'] = $post['processId'];
		$data['data'] = $post['data'];
				
		$data['category'] = $post['categoryid'];
		
		if ($update) {
			$data['id'] = $update;
		}
		
		if (!$row->bind($data)) {
			$this->setError($this->_db->getErrorMsg());
			return false;
		}

		if (!$row->store()) {
			$this->setError($this->_db->getErrorMsg());
			return false;
		}
		
		return true;
	}
	
	private function _checkId($post)
	{	
		$db =& JFactory::getDBO();
		$query = 'SELECT id'
			   .' FROM #__syncr_content'
			   .' WHERE process_id = '.$post['processId']
			   ;
		$db->setQuery($query);
	
		return $db->loadResult();
	}
}